import checkStatus from '@/utils/status';
import * as api from '../api/report';

export default {
  namespace: 'report',
  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(api.List, payload);
      yield checkStatus(response);
      yield put({
        type: 'setList',
        payload: response.msg,
      });
    },
    * add({ payload, callback }, { call, put }) {
      const response = yield call(api.add, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
    * update({ payload, callback }, { call, put }) {
      const response = yield call(api.update, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },

    * upload({ payload, callback }, { call, put }) {
      const response = yield call(api.uploadFile, payload);
      if (callback) callback(response);
    },
    * status({ payload, callback }, { call, put }) {
      const response = yield call(api.status, payload);
      if (callback) callback(response);
    },
    * remove({ payload, callback }, { call, put }) {
      const response = yield call(api.remove, payload);
      if (callback) callback(response);
    },
    * getDesc({ payload, callback }, { call, put }) {
      const response = yield call(api.Detail, payload);
      yield checkStatus(response);
      yield put({
        type: 'getdesc',
        payload: response.msg,
      });
      if (callback) callback();
    },
  },
  reducers: {
    setList(state, action) {
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total,
      };
      return {
        ...state,
        data: action.payload,
      };
    },
    getdesc(state, action) {
      return {
        ...state,
        Desc: action.payload,
      };
    },
  },
};
